/**
 * Created by User on 27.07.2015.
 */
var app = angular.module("moneyApp",["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider.when('/operations',
        {
            templateUrl: 'resources/templates/operations.html',
            controller: 'OperationsCtrl'
        });
    $routeProvider.when('/changeAccounts',
        {
            templateUrl: 'resources/templates/changeAccounts.html',
            controller: 'ChangeAccountsCtrl'
        });

    });
app.controller('OperationsCtrl',
    function OperationsCtrl($scope, $http) {

        getAccounts();
        function getAccounts(){
            $http.get('?getAccounts').success(function(data) {
                $scope.accounts=data;
                $scope.accountsIndex={};
                createIndexObj($scope.accountsIndex,data);
            });
        }
        function createIndexObj(obj,data){
            for (var i=0; i<data.length;i++){
                obj[data[i].id]=data[i];
            }
        }
        $http.get('?getOperations').success(function(data) {
            $scope.operations=data;
        });
        $http.get('?getIncomingCategories').success(function(data) {
            $scope.incomeCategories=data;
            $scope.incomeCategoriesIndex={};
            createIndexObj( $scope.incomeCategoriesIndex,data);
        });

        $http.get('?getOutgoingCategories').success(function(data) {
            $scope.expenseCategories=data;
            $scope.expenseCategoriesIndex={};
            createIndexObj( $scope.expenseCategoriesIndex,data);
        });
        $scope.operation={};
        $scope.operation.date=(+new Date);
        $scope.changeDate=function(n){
            $scope.operation.date = $scope.operation.date+n*86400000;
        };
        $scope.operation.type='expense';

        $scope.operation.comment="";

        $scope.eraseForm = function() {
            var type=$scope.operation.type;
            $scope.operation={};
            $scope.operation.date=(+new Date);
            $scope.operation.type=type;
          
        };
        $scope.addOperation = function(){
            $http.post('addOperation',$scope.operation).then(
                function (response) {
                    $scope.operations = response.data;
                    $scope.eraseForm();
                },
                function(response){
                    alert(response.data.errorMessage)
                });

        };

        $scope.deleteOperation = function(id){
            if (id !== undefined && confirm("sure?!")) {
                $http.get('?deleteOperation&id=' + id).success(function (data) {
                    $scope.operations = data;

                    $scope.eraseForm();
                });
            }
        };


        $scope.setOperation=function(operation){
            $scope.operation=$.extend({}, operation);


        };
        $scope.setFile = function () {
          return  'resources/templates/'+$scope.operation.type+'.html';
        };
        $scope.setMode=function(mode){
            $scope.operation.type=mode;
            $scope.eraseForm();
        }

    });

    app.controller('ChangeAccountsCtrl',
    function ChangeAccountsCtrl($scope, $http){

        $http.get('?getAccounts').then(function(response) {
            $scope.accounts=response.data;
        });

        $scope.delete = function(id){
            if (id !== undefined && confirm("sure?!")) {
                $http.get('?deleteAccount&id=' + id).success(function (data) {
                    $scope.accounts = data;
                });
            }
        };

        $scope.add = function(account){
            $http.post('add_account',account).then(
                function (response) {

                    $scope.accounts = response.data
                },
                function(response){
                alert(response.data.errorMessage)
            });
            $scope.newAccount={};
        };

        $scope.edit = function(account) {
            $scope.activeAccount = account;
        };
    }

);

app.directive("orderedList", function () {
    return {
        link: function (scope, element, attrs) {
            scope.openCategories=function(){
               scope.isOpen=!scope.isOpen;
            };


            scope.chooseCategory = function(category){
                scope.id=category.id;

                scope.isOpen=false;
                scope.pattern="";

            };
            scope.matchId = function(query) {
                return function(category) { return (category.parentId==query); }
            };
        },
        restrict: "A",
        templateUrl: "resources/templates/categories.html",
        scope: {
            categories:"=orderedList",
            id:"=id",
            editable:"=editable",
            index:"=index",
            isOpen:"=open"
        }

    }

});

